<?php
if (isset($_REQUEST["id"]) && getTransactionPermission($_REQUEST["id"], $_SESSION["userid"]))
{
	if (isset($_REQUEST["split"]))
	{
		$success = true;
		debug($_REQUEST);
/*		$validator = array("bookingtime" => "isPostedDatetime",
							"amount" => "isPostedInteger",
							"accountid" => "isPosted",
							"categoryid" => "isPosted");

		if (validateForm($validator, $_REQUEST))
		{
			if (!modifyTransaction($_REQUEST["transactionid"], $_REQUEST["accountid"], $_SESSION["userid"], $_REQUEST["direction"], $_REQUEST["amount"], $_REQUEST["description"], $_REQUEST["bookingtime"], $_REQUEST["categoryid"], $_REQUEST["tags"], $_REQUEST["partnername"]))
			{
				$success = false;
			}
		}
		else
		{
			$success = false;
		}*/
	}

	$selectedTransaction = getTransaction($_REQUEST["id"]);
	$accounts = getAccounts($_SESSION["userid"]);
	$categoryTree = getCategoryTree("NULL", 1, false);
?>
			<div id="textcontainer">
				<h2>Tranzakció felosztása</h2>
				<p>
					<?php if (isset($success) && !$success) print "<div class=\"errortitle\">valami nem sikerult!</div>";
						else if (isset($success)) print "<div class=\"errortitle\">Sikerult!</div>";?>
					<form name="newtransaction" action="<?php print $_SERVER["SCRIPT_NAME"] . "?" . $_SERVER["QUERY_STRING"];?>" method="post">
						<input type="hidden" name="transactionid" value="<?php print $_REQUEST["id"];?>"/>
						számla: 
<?php
	foreach ($accounts as $acc)
	{
		if($selectedTransaction["accountid"] == $acc["id"]) print $acc["name"];
	}
?>
						<br/>
						irány: <?php print (($selectedTransaction["amount"] < 0)?"kiadás":"bevétel");?><br/>
						teljes összeg: <?php print abs($selectedTransaction["amount"]);?><br/>
						partner: <?php print $selectedTransaction["partnername"];?><br/>
						leírás: <?php print $selectedTransaction["description"];?><br/>
						időpont: <?php print $selectedTransaction["bookingtime"];?><br/>
						címke: <?php print $selectedTransaction["tags"];?><br/>
<?php
	if (isset($selectedTransaction["split"]))
	{
		foreach($selectedTransaction["split"] as $split)
		{
			print "<hr/>";
			print 'összeg: <input type="text" name="splitamount[]" value="' . abs($split["splitamount"]) . '"><br/>';
			print 'kategória: <select name="categoryid[]">';
			foreach ($categoryTree as $category)
			{
				print "<option value=\"" . $category["category"]["id"] . "\"" . (($split["categoryid"] == $category["category"]["id"])?" selected=\"selected\"":"") . ">" . $category["category"]["name"] . "</option>";
				if (isset($category["subcategoryList"]))
				{
					foreach($category["subcategoryList"] as $subcategory)
					{
						print "<option value=\"" . $subcategory["category"]["id"] . "\"" . (($split["categoryid"] == $subcategory["category"]["id"])?" selected=\"selected\"":"") . ">&nbsp;&nbsp; - " . $subcategory["category"]["name"] . "</option>";
					}
				}
			}
			print '</select>';
		}
	}
	else
	{
		print "<hr/>";
		print 'összeg: <input type="text" name="splitamount[]" value="' . abs($selectedTransaction["amount"]) . '"><br/>';
		print 'kategória: <select name="categoryid[]">';
		foreach ($categoryTree as $category)
		{
			print "<option value=\"" . $category["category"]["id"] . "\"" . (($selectedTransaction["categoryid"] == $category["category"]["id"])?" selected=\"selected\"":"") . ">" . $category["category"]["name"] . "</option>";
			if (isset($category["subcategoryList"]))
			{
				foreach($category["subcategoryList"] as $subcategory)
				{
					print "<option value=\"" . $subcategory["category"]["id"] . "\"" . (($selectedTransaction["categoryid"] == $subcategory["category"]["id"])?" selected=\"selected\"":"") . ">&nbsp;&nbsp; - " . $subcategory["category"]["name"] . "</option>";
				}
			}
		}
		print '</select>';
	}
	for ($i = 0; $i < 3; $i++)
	{
		print "<hr/>";
		print 'összeg: <input type="text" name="splitamount[]" value=""><br/>';
		print 'kategória: <select name="categoryid[]">';
		print '<option value="">- válassz -</option>';
		foreach ($categoryTree as $category)
		{
			print "<option value=\"" . $category["category"]["id"] . "\">" . $category["category"]["name"] . "</option>";
			if (isset($category["subcategoryList"]))
			{
				foreach($category["subcategoryList"] as $subcategory)
				{
					print "<option value=\"" . $subcategory["category"]["id"] . "\">&nbsp;&nbsp; - " . $subcategory["category"]["name"] . "</option>";
				}
			}
		}
		print '</select>';
	}
?>
						<br/><input type="submit" name="split" value="Felbont"/>
					</form>
				</p>
			</div>
			<!---->
<?php
}
else
{
	print '<div id="textcontainer">nincs hozzáférési joga</div>';
}
?>
